package com.example.app;

import com.example.app.entity.Address;
import com.example.app.entity.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.*;
import org.springframework.test.context.junit4.SpringRunner;

import javax.annotation.Resource;
import java.util.concurrent.TimeUnit;

/**
 * @author wangzhuangzhuang
 * @DESC:
 * @date 2022-02-10 16:39
 */
@SpringBootTest
@RunWith(SpringRunner.class)
public class RedisConfigTest2 {

	@Resource
	private ValueOperations<String,Object> valueOperations;   //以redis string类型存取Java Object(序列化反序列化)

	@Resource
	private HashOperations<String, String, Object> hashOperations; //以redis的hash类型存储java Object

	@Resource
	private ListOperations<String, Object> listOperations; //以redis的list类型存储java Object

	@Resource
	private SetOperations<String, Object> setOperations;   //以redis的set类型存储java Object

	@Resource
	private ZSetOperations<String, Object> zSetOperations;  //以redis的zset类型存储java Object

	@Test
	public void testValueObj(){
		Person person = new Person("boke","byrant");
		person.setAddresses(new Address("南京","中国"));
		//向redis数据库保存数据(key,value),数据有效期20秒
		valueOperations.set("player:1",person,20, TimeUnit.SECONDS); //20秒之后数据消失
		//根据key把数据取出来
		Person getBack = (Person)valueOperations.get("player:1");
		System.out.println(getBack);
	}

}